package com.zw.pdm.module.designtask.service;

import com.zw.pdm.entity.designtask.DsnControlIntent;
import com.zw.pdm.module.designtask.mapper.DesignTaskMapper;
import com.zw.pdm.module.designtask.mapper.DsnConIntMapper;
import com.zw.pdm.module.designtask.repository.DsnControlIntentRepository;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.FileUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * @Description:
 * @Author: China.wsg
 * @Date: 2020/11/03 09:00
 */
@Service
@SuppressWarnings("all")
public class DsnConIntService {

    @Autowired
    private DsnConIntMapper dsnConIntMapper;
    @Autowired
    private DsnControlIntentRepository dsnControlIntentRepository;
    @Autowired
    private LogService logService;
    @Value("${user.upload.path}")
    private String uploadPath;
    @Autowired
    private DesignTaskMapper designTaskMapper;


    /**
     * 控制意图列表
     *
     * @param proCode
     * @return
     */
    public List<DsnControlIntent> findControlIntentListByQuery(DsnControlIntent dsnControlIntent) {
        return dsnConIntMapper.findControlIntentListByQuery(dsnControlIntent);
    }

    /**
     * 上传附件
     *
     * @param file 上传附件
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void uploadFile(MultipartFile file, Integer proId, String fileContent, Integer fileType) {
        // 取得项目信息
        Project project = designTaskMapper.findProjectById(proId);
        String path = "";
        if (fileType == 1) {
            path = uploadPath + project.getCode() + "/" + "controlIntent/";
        } else if (fileType == 3) {
            path = uploadPath + project.getCode() + "/" + "controlDebug/";
        }
        String fileName = FileUtils.upload(file, path);
        String url = "";
        if (fileType == 1) {
            url = "/files/" + project.getCode() + "/" + "controlIntent/" + fileName;
        } else if (fileType == 3) {
            url = "/files/" + project.getCode() + "/" + "controlDebug/" + fileName;
        }

        DsnControlIntent dsnControlIntent = new DsnControlIntent();
        dsnControlIntent.setProId(project.getId());
        dsnControlIntent.setProCode(project.getCode());
        dsnControlIntent.setProName(project.getName());
        dsnControlIntent.setFileName(fileName);
        dsnControlIntent.setFileContent(fileContent);
        dsnControlIntent.setUrl(url);
        dsnControlIntent.setFileType(fileType);
        dsnControlIntent.setCreateCode(ShiroUtils.getUser().getAccount());
        dsnControlIntent.setCreateName(ShiroUtils.getUser().getName());
        dsnControlIntentRepository.save(dsnControlIntent);
        logService.saveLog(fileType == 1 ? "控制意图上传" : "调试任务上传", project.getCode() + "<br>" + fileName);
    }

    /**
     * 下载文件
     */
    public void download(HttpServletResponse response, Integer[] id, String proCode) {
        if (id.length == 1) {
            DsnControlIntent controlIntent = dsnControlIntentRepository.findById(id[0]).get();
            logService.saveLog("控制意图下载", "项目名：" + controlIntent.getProName() + "<br>文件名：" + controlIntent.getFileName());
            String path = uploadPath + controlIntent.getUrl().replace("/files/", "");
            File file = new File(path);
            FileUtils.downloadFile(file, response);
        } else {
            List<String> list = new ArrayList<>();
            DsnControlIntent controlIntent;
            for (Integer integer : id) {
                controlIntent = dsnControlIntentRepository.findById(integer).get();
                logService.saveLog("控制意图下载", "项目名：" + controlIntent.getProName() + "<br>文件名：" + controlIntent.getFileName());
                String path = uploadPath + controlIntent.getUrl().replace("/files/", "");
                list.add(path);
            }
            FileUtils.zipFileAll(list, response);
        }
    }

    /**
     * 删除控制意图
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int delConIntentById(Integer[] ids) {

        for (Integer id : ids) {
            DsnControlIntent dsnControlIntent = dsnControlIntentRepository.getOne(id);
            logService.saveLog("删除控制意图", "项目名称：" + dsnControlIntent.getProName() + "<br>文件名称：" + dsnControlIntent.getFileName());
            String path = uploadPath + dsnControlIntent.getUrl().replace("/files/", "");
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
            dsnControlIntentRepository.deleteById(id);
        }
        return ids.length;
    }


}
